package com.sdk.demo;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import anm.union.BSDK;
import anm.union.common.CallBack;

public class MainActivity extends Activity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewInit();
        BSDK.onCreate(savedInstanceState);
        //初始化
        BSDK.mainInit(this, new CallBack() {
            @Override
            public void onResult(Map<String, String> data, boolean isSucess) {
                if (isSucess) {
                    // 初始化成功
                }
            }
        }, new CallBack() {
            @Override
            public void onResult(Map<String, String> data, boolean isSucess) {
                if (isSucess) {
                    // 登出（注销）成功
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //登录
            case R.id.btn1:
                BSDK.login(new CallBack() {
                    @Override
                    public void onResult(Map<String, String> data, boolean isSucess) {
                        if (isSucess) {
                            Log.d(TAG, "登录成功：" + data);
                        }
                    }
                });
                break;
            //支付
            case R.id.btn2:
                Map<String, String> payInfo = new HashMap<>();
                payInfo.put("order_id", System.currentTimeMillis() + "");       // 订单号
                payInfo.put("money", "600");                                    // 钱，单位分
                payInfo.put("product_id", "167");                               // 商品id
                payInfo.put("product_des", "钻石");                             // 商品描述
                payInfo.put("product_count", "60");                             // 商品数量
                payInfo.put("server_id", "8848");                               // 服务器id
                payInfo.put("server_name", "8848pro");                          // 服务器名字
                payInfo.put("role_name", "小明");                               // 角色名字
                payInfo.put("role_id", "169");                                  // 角色id
                payInfo.put("role_level", "99");                                // 角色等级,无的话可传0
                payInfo.put("notify_url", "http://www.baidu.com");              // 支付通知回调地址
                payInfo.put("custom", "透传透传");                               // 透传
                BSDK.pay(payInfo);
                break;
            //创角上报
            case R.id.btn3:
                BSDK.reportData(commonRoleInfo("1"));
                break;
            //升级上报
            case R.id.btn4:
                BSDK.reportData(commonRoleInfo("2"));
                break;
            //进入游戏上报
            case R.id.btn5:
                BSDK.reportData(commonRoleInfo("3"));
                break;
            //切换账号（注销）
            case R.id.btn8:
                // 回调在初始化接口
                BSDK.loginOut();
                break;
            //退出游戏
            case R.id.btn9:
                BSDK.exit(new CallBack() {
                    @Override
                    public void onResult(Map<String, String> data, boolean isSucess) {
                        //关闭游戏
                        MainActivity.this.finish();
                        android.os.Process.killProcess(android.os.Process.myPid());
                        System.exit(0);
                    }
                });
                break;
        }
    }

    private Map<String, String> commonRoleInfo(String reportType) {
        Map<String, String> roleParam = new HashMap<>();
        roleParam.put("report_type", reportType);                   // 上报类型,创角填"1"，升级填"2",进入游戏填"3"

        roleParam.put("server_id", "10086");                        // 服务器id
        roleParam.put("server_name", "10086s");                     // 服务器名称
        roleParam.put("role_id", "8848");                           // 角色id
        roleParam.put("role_name", "小强");                         // 角色名称
        roleParam.put("role_level", "99");                          // 角色等级
        roleParam.put("role_sex", "男");                            // 角色性别，填"男"或"女"
        roleParam.put("role_power", "5");                           // 角色战力
        roleParam.put("role_create_time", "1630655714");            // 角色创建时间，秒级时间戳
        roleParam.put("role_vip_level", "1");                       // 角色vip等级
        roleParam.put("party_id", "2");                             // 帮派（公会）id
        roleParam.put("party_name", "学习公会");                    // 帮派（公会）名称
        return roleParam;
    }

    private void viewInit() {
        findViewById(R.id.btn1).setOnClickListener(this);
        findViewById(R.id.btn2).setOnClickListener(this);
        findViewById(R.id.btn3).setOnClickListener(this);
        findViewById(R.id.btn4).setOnClickListener(this);
        findViewById(R.id.btn5).setOnClickListener(this);
        findViewById(R.id.btn6).setOnClickListener(this);
        findViewById(R.id.btn7).setOnClickListener(this);
        findViewById(R.id.btn8).setOnClickListener(this);
        findViewById(R.id.btn9).setOnClickListener(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        BSDK.onResume();
    }

    @Override
    public void onRestart() {
        super.onRestart();
        BSDK.onRestart();
    }

    @Override
    public void onStart() {
        super.onStart();
        BSDK.onStart();
    }

    @Override
    public void onPause() {
        super.onPause();
        BSDK.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        BSDK.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BSDK.onDestroy();
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        BSDK.onNewIntent(intent);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        BSDK.onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        BSDK.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

}